//////////////////////////////////////////////////////////////////////////

#ifndef _SERVERMAPPARSER_H_
#include "servermapparser.h"
#endif

//////////////////////////////////////////////////////////////////////////


ServerMapParser::ServerMapParser( const char* _fileName )
: MapParserBase(_fileName)
{
}


//////////////////////////////////////////////////////////////////////////


ServerMapParser::~ServerMapParser()
{
}


//////////////////////////////////////////////////////////////////////////


void ServerMapParser::ParseCircle()
{
    // Read shape absolute position
    Point pt = ParsePosition();

    // Read radius
    float radius;
    m_FileStream >> radius;

    // Convert to physics coordinates
    radius = ScreenToPhysics(radius);

    b2CircleShape circle;
    circle.m_radius = radius;

    m_Map.m_Circles.push_back(std::make_pair(circle, pt) );
}


//////////////////////////////////////////////////////////////////////////


void ServerMapParser::ParseRect()
{
    // Read shape absolute position
    Point pt = ParsePosition();

    // Read size
    float width, height;
    m_FileStream >> width >> height;

    // Convert to physics coordinates
    width = ScreenToPhysics(width);
    height = ScreenToPhysics(height);

    // Create rectangle
    b2PolygonShape rect;
    rect.SetAsBox(width, height);

    m_Map.m_Rects.push_back(std::make_pair(rect, pt) );
}


//////////////////////////////////////////////////////////////////////////


void ServerMapParser::ParsePolygon()
{
    // Read shape absolute position
    Point pt = ParsePosition();
    
    // Read number of vertexes
    int n;
    m_FileStream >> n;

    //polygon.m_vertexCount = n;
    b2Vec2* vertexes = new b2Vec2[n];

    // Read all vertexes
    for ( int i = 0; i < n; i++ )
    {
        Point pt = ParsePosition();
        vertexes[i].Set(pt.x, pt.y);
    }

    // Create polygon
    b2PolygonShape polygon;
    polygon.Set(vertexes, n);

    delete[] vertexes;

    m_Map.m_Polygons.push_back(std::make_pair(polygon, pt) );
}


//////////////////////////////////////////////////////////////////////////


void ServerMapParser::ParseWall()
{
    // Read shape absolute position
    Point pt = ParsePosition();

    // Create rectangular wall
    b2PolygonShape rect;
    rect.SetAsBox(1.0f, 1.0f);

    m_Map.m_Rects.push_back(std::make_pair(rect, pt) );
}


//////////////////////////////////////////////////////////////////////////


Point ServerMapParser::ParsePosition()
{
    // Read absolute position
    Point pt;
    m_FileStream >> pt.x >> pt.y;

    // Convert to physics coordinates
    pt.x = ScreenToPhysics(pt.x);
    pt.y = ScreenToPhysics(pt.y);

    return pt;
}


//////////////////////////////////////////////////////////////////////////


void ServerMapParser::SetMapSize( int _w, int _h )
{
    m_Map.m_Width = ScreenToPhysics(_w);
    m_Map.m_Height = ScreenToPhysics(_h);

    // Set map corner points
    Point pt[4];
    pt[0].x = 0.0f;
    pt[0].y = 0.0f;

    pt[1].x = m_Map.m_Width;
    pt[1].y = 0.0f;

    pt[2].x = m_Map.m_Width;
    pt[2].y = m_Map.m_Height;

    pt[3].x = 0.0f;
    pt[3].y = m_Map.m_Height;

    // Create invisible map borders
    for ( int i = 0; i < 4; i++ )
    {
        b2Vec2 vertexes[2];
        vertexes[0].Set(pt[i].x, pt[i].y);
        vertexes[1].Set(pt[(i+1)%4].x, pt[(i+1)%4].y);

        b2PolygonShape polygon;
        polygon.Set(vertexes, 2);
        m_Map.m_Polygons.push_back(std::make_pair(polygon, pt[0]) );
    }
}


//////////////////////////////////////////////////////////////////////////


void ServerMapParser::ParseRevivalPoints()
{
    // Number of revival points
    int n;
    m_FileStream >> n;

    // Read all points
    for ( int i = 0; i < n; i++ )
        m_Map.m_RevivalPoints.push_back(ParsePosition());
}


//////////////////////////////////////////////////////////////////////////


MapOnServer& ServerMapParser::GetMap()
{
    return m_Map;
}


//////////////////////////////////////////////////////////////////////////
